import random
from django.contrib.auth.hashers import make_password
from datetime import timedelta
from users.models import User
from tasks.models import Tasks
from django.utils import timezone
from django.conf import settings


def create_password():
    """ simple password generator"""
    chars = '!&$#?abcdefghijknopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789'

    password = ''.join(random.choice(chars) for _ in range(8))
    hashed_password = make_password(password)

    return password, hashed_password


def create_empty_tasks(year, count, task_owner):
    for _ in range(count):
        task = Tasks()
        task.year = year
        task.owner = task_owner
        task.save()
    return None

def validate_email(email, user=None):
    try:
        if user := User.objects.get(email=email):
            return False, False
    except Exception:
        username, domain = email.split('@')
        username.replace('.', '')
        username.capitalize()
        domain_name = domain.split('.')[-1]

        if domain_name in ('ru', 'com', 'by', 'рф', 'su'):
            return True, username
        else:
            return False, False
        

def check_tasks_creation_time(value):
    """
    Определяет возможность добавления пустых ячеек согласно временному диапазону
    """
    minutes_period_of_available_change = settings.EMPTY_TASK_ADD_TIME_LIMIT  # 10 minuts

    now = timezone.now()
    if (value + timedelta(minutes=minutes_period_of_available_change)) > now:
        return False
    return True